package com.mew.chess.core.game.bjl.game.core;

import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlBetType;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlResultBO;
import com.mew.chess.core.game.bjl.game.message.PushOpenCardMessage;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.common.util.NumberUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dylin
 * @create 2021/4/22
 */
public class BjlCardUtil {
    private static final int MAX_CARD=52;
    private static final List<Integer> cardLib=new ArrayList<>();

    /**
     * 洗牌
     * @return
     */
    public static List<Integer> getShuffleCardLib(){
        if(cardLib.size()==0){
            //把所有牌添加进牌库
            for (int p = 0; p < BjlConstantValue.PAIR; p++) {
                for(int c=0;c<MAX_CARD;c++){
                    cardLib.add(c);
                }
            }
        }
        Collections.shuffle(cardLib);
        return cardLib;
    }
    /**
     * 计算牌的面值
     * @return
     */
    public static int computeCardVal(Integer card){
        int val=card%13+1;
        if(val>10){
            val=0;
        }
        return val;
    }

    /**
     * 计算手牌大小
     * @param cards 牌
     * @return
     */
    public static int computeCardsVal(List<Integer> cards){
        int val=0;
        for (Integer card : cards) {
            val+= computeCardVal(card);
        }
        if(val>9){
            val=val%10;
        }
        return val;
    }

    /**
     * 是否是天牌
     * @param cards
     * @return
     */
    public static boolean isMajor(List<Integer> cards){
        return computeCardsVal(cards)>7;
    }

    public static void main(String[] args) {
        int time=1000000;
        int bankerWin=0;
        int idleWin=0;
        int equal=0;
        int bankerTwo=0;
        int idleTwo=0;
        List<Integer> cards=new ArrayList<>();
        for (int i = 0; i < time; i++) {
            if(cards.size()<7){
                int cardNumber= NumberUtil.randomIntByStr(BjlConstantValue.USE_CARD_NUMBER_SEC,"-");
                cards=new ArrayList<>(BjlCardUtil.getShuffleCardLib().subList(0,cardNumber).stream().collect(Collectors.toList()));
            }
            BjlRoomManager roomManager = new BjlRoomManager(0,0);
            BjlCardDevice.dealCard(roomManager);
            BjlResultBO resultBO=roomManager.getBjlResult();
            BjlGameUtil.validWin(resultBO);
            if(resultBO.getWinList().contains(BjlBetType.BankerWin.code())){
                bankerWin++;
            }
            if(resultBO.getWinList().contains(BjlBetType.IdleWin.code())){
                idleWin++;
            }
            if(resultBO.getWinList().contains(BjlBetType.Equal.code())){
                equal++;
            }
            if(resultBO.getWinList().contains(BjlBetType.BankerTwo.code())){
                bankerTwo++;
            }
            if(resultBO.getWinList().contains(BjlBetType.IdleTwo.code())){
                idleTwo++;
            }
        }
        System.out.println("总次数："+time);
        System.out.println("庄赢次数："+bankerWin);
        System.out.println("闲赢次数："+idleWin);
        System.out.println("和赢次数："+equal);
        System.out.println("庄对次数："+bankerTwo);
        System.out.println("闲对次数："+idleTwo);
    }
}
